#include "level.h"
#include "gamemodel.h"
#include <cstdlib>

Level::Level(int width, int height) {
	this->height = height;
	this->width = width;
	this->levelData = new char[height * width];

	for (int i = 0; i < (height * width); i++) {
		this->levelData[i] = '-';
	}

}

Level::~Level(void) {
	delete[] this->levelData;
}

char Level::getTile(int x, int y) {
	if ((y * this->width + x) < (height * this->width))
		return this->levelData[y * this->width + x];

	return 0;
}

char Level::getNearestTile(float x, float y) {
	int x1 = static_cast<int> (x);
	int y1 = static_cast<int> (y);

	return Level::getTile(x1, y1);
}

void Level::setTile(int x, int y, char data) {
	if ((y * this->width + x) < (this->height * this->width)) {
		this->levelData[y * this->width + x] = data;

		//Startposition merken
		if (data == PLAYER_START) {
			this->startX = x;
			this->startY = y;
		}
		if (data == ENEMY_MOVE_FIVE || data == ENEMY_MOVE_THREE) {
			Enemy* e =
					new Enemy(static_cast<float> (x), static_cast<float> (y));
			e->setPosX(static_cast<float> (x));
			e->setPosY(static_cast<float> (y));
			e->setRange(atoi(&data));
#ifndef WIN32
			e->setRange(e->getRange() - 1);
#endif
			GameModel::getInstance()->addEnemy(e);
		}
	}
}

void Level::printLevelToConsole() {
	for (int y = 0; y < this->height; y++) {
		for (int x = 0; x < this->width; x++) {
///			CL_Console::write(std::string(1, this->getTile(x, y)));
		}
///		CL_Console::write("\n");
	}
}

int Level::getStartX(void) {
	return this->startX;
}

int Level::getStartY(void) {
	return this->startY;
}

int Level::getHeight() {
	return this->height;
}

int Level::getWidth() {
	return this->width;
}
